/*************************************************************************
 *
 *   file		: Summon.cs
 *   copyright		: (C) The WCell Team
 *   email		: info@wcell.org
 *   last changed	: $LastChangedDate: 2009-09-16 16:25:58 +0100 (Wed, 16 Sep 2009) $
 *   last author	: $LastChangedBy: dominikseifert $
 *   revision		: $Rev: 1102 $
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *************************************************************************/

using WCell.Constants;
using WCell.Constants.NPCs;
using WCell.Constants.Pets;
using WCell.Constants.Skills;
using WCell.Constants.Spells;
using WCell.RealmServer.Entities;
using WCell.RealmServer.Handlers;
using WCell.RealmServer.NPCs;
using WCell.Util.Graphics;

namespace WCell.RealmServer.Spells.Effects
{
	/// <summary>
	/// Summons a friendly companion, Pets, Guardians or Totems
	/// TODO: Handle Totems
	/// </summary>
	public class SummonEffectHandler : SpellEffectHandler
	{
		protected NPCEntry entry;

		public SummonEffectHandler(SpellCast cast, SpellEffect effect)
			: base(cast, effect)
		{
			// MiscValueB:
			// 41 -> NonCombat Companion
			// 
		}

		public override void Initialize(ref SpellFailedReason failReason)
		{
			var id = (NPCId)Effect.MiscValue;
			entry = NPCMgr.GetEntry(id);
			if (entry == null)
			{
				failReason = SpellFailedReason.Error;
				return;
			}
		}

		public virtual SummonType SummonType
		{
			get { return (SummonType)Effect.MiscValueB; }
		}

		public override void Apply()
		{
			var caster = m_cast.CasterUnit;

			Vector3 targetLoc;
			if (m_cast.TargetLoc.X != 0)
			{
				targetLoc = m_cast.TargetLoc;
			}
			else
			{
				targetLoc = caster.Position;
			}

			var handler = SpellHandler.GetSummonHandler(SummonType);
			var pet = handler.Summon(m_cast, ref targetLoc, entry);

			pet.CreationSpellId = Effect.Spell.SpellId;
		}

		public override ObjectTypes CasterType
		{
			get { return ObjectTypes.Unit; }
		}

		public override bool HasOwnTargets
		{
			get
			{
				return false;
			}
		}
	}
}
